//
//  PlayVC.m
//  New
//
//  Created by 才诗琦 on 16/1/25.
//  Copyright © 2016年 www.lanou3g.com. All rights reserved.
//

#import "PlayVC.h"
#import <AVFoundation/AVFoundation.h>
@interface PlayVC ()

@end

@implementation PlayVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    [self setupUI];
//        [self.player play];
}
#pragma mark - 私有方法

//- (void)setupUI
//{
//
//
//    //创建播放器层
////    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
////    playerLayer.frame = self.container.frame;
////
////  [self.container.layer addSublayer:playerLayer];
////
//
//
//}
//
///**
// *截取指定时间的视频缩略图
// *timeBySecond 时间点
// */
//
///**
// *初始化播放器
// */
//
//-(AVPlayer *)player{
//    if (!_player) {
//        AVPlayerItem *playerItem=[self getPlayItem:0];
//        _player=[AVPlayer playerWithPlayerItem:playerItem];
//        [self addProgressObserver];
//        [self addObserverToPlayerItem:playerItem];
//    }
//    return _player;
//}
//
///**
// *  根据视频索引取得AVPlayerItem对象
// *
// *  @param videoIndex 视频顺序索引
// *
// *  @return AVPlayerItem对象
// */
//-(AVPlayerItem *)getPlayItem:(int)videoIndex{
//    NSString *urlStr=[NSString stringWithFormat:self.str,videoIndex];
//    urlStr =[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//    NSURL *url=[NSURL URLWithString:urlStr];
//    AVPlayerItem *playerItem=[AVPlayerItem playerItemWithURL:url];
//    return playerItem;
//}
//#pragma mark - 通知
///**
// *  添加播放器通知
// */
//-(void)addNotification{
//    //给AVPlayerItem添加播放完成通知
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.player.currentItem];
//}
//
//-(void)removeNotification{
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
//}
//
///**
// *  播放完成通知
// *
// *  @param notification 通知对象
// */
//-(void)playbackFinished:(NSNotification *)notification{
//    NSLog(@"视频播放完成.");
//}
//
//#pragma mark - 监控
///**
// *  给播放器添加进度更新
// */
//-(void)addProgressObserver{
//    AVPlayerItem *playerItem=self.player.currentItem;
//    UIProgressView *progress=self.progress;
//    //这里设置每秒执行一次
//    [self.player addPeriodicTimeObserverForInterval:CMTimeMake(1.0, 1.0) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
//        float current=CMTimeGetSeconds(time);
//        float total=CMTimeGetSeconds([playerItem duration]);
//        NSLog(@"当前已经播放%.2fs.",current);
//        if (current) {
//            [progress setProgress:(current/total) animated:YES];
//        }
//    }];
//}
//
///**
// *  给AVPlayerItem添加监控
// *
// *  @param playerItem AVPlayerItem对象
// */
//-(void)addObserverToPlayerItem:(AVPlayerItem *)playerItem{
//    //监控状态属性，注意AVPlayer也有一个status属性，通过监控它的status也可以获得播放状态
//    [playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
//    //监控网络加载情况属性
//    [playerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];
//}
//-(void)removeObserverFromPlayerItem:(AVPlayerItem *)playerItem{
//    [playerItem removeObserver:self forKeyPath:@"status"];
//    [playerItem removeObserver:self forKeyPath:@"loadedTimeRanges"];
//}
///**
// *  通过KVO监控播放器状态
// *
// *  @param keyPath 监控属性
// *  @param object  监视器
// *  @param change  状态改变
// *  @param context 上下文
// */
//-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
//    AVPlayerItem *playerItem=object;
//    if ([keyPath isEqualToString:@"status"]) {
//        AVPlayerStatus status= [[change objectForKey:@"new"] intValue];
//        if(status==AVPlayerStatusReadyToPlay){
//            NSLog(@"正在播放...，视频总长度:%.2f",CMTimeGetSeconds(playerItem.duration));
//        }
//    }else if([keyPath isEqualToString:@"loadedTimeRanges"]){
//        NSArray *array=playerItem.loadedTimeRanges;
//        CMTimeRange timeRange = [array.firstObject CMTimeRangeValue];//本次缓冲时间范围
//        float startSeconds = CMTimeGetSeconds(timeRange.start);
//        float durationSeconds = CMTimeGetSeconds(timeRange.duration);
//        NSTimeInterval totalBuffer = startSeconds + durationSeconds;//缓冲总长度
//        NSLog(@"共缓冲：%.2f",totalBuffer);
//        //
//    }
//}
//
//
//


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
